<template>
  <div class="back">
    
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm form"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button type="success" @click="register">注册</el-button>
        <span class="forge" @click="forgets">忘记密码</span>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { mapActions } from "vuex";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "admin",
        password: "111111"
      },
      rules: {
        username: [{ validator: validateName, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }]
      }
    };
  },
  methods: {
    ...mapActions("Logins", ["login"]),
    forgets() {
      this.$router.push("/forget");
    },
    register() {
      this.$router.push("/register");
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.login(this.ruleForm).then(res => {
            console.log(res.data)
            if (res.data.errorCode == 0 && res.data.data.status == 1) {
              this.$message("登录成功！！！");
              this.$router.push("/home");
            } else if (res.data.code == 10000) {
              this.$message("登录超时！！！");
            } else if (res.data.errorCode == 401) {
              this.$message("密码或账号错误");
            } else {
              this.$message("用户没有登录权限");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style  scoped>
.back {
  width: 100%;
  height: 100%;
  background-size: 100% 100%;
  background-image: url(../assets/back.jpg);
}
.form {
  position: absolute;
  width: 35%;
  top: 45%;
  left: 20%;
}
.forge {
  cursor: pointer;
  color: blue;
}
</style>